//
//  GYCitySelectViewController.m
//  GoodLuck
//
//  Created by apple on 2018/1/30.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYCitySelectViewController.h"
#import "GYRegisterCell.h"
#import "GYCity.h"

@interface GYCitySelectViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *provineceBtu;

@property (weak, nonatomic) IBOutlet UIButton *cityBtu;
@property (weak, nonatomic) IBOutlet UIButton *areaBtu;

/** 所有的省 */
@property (nonatomic, strong) NSArray *provinces;

/** 省下面的所有市 */
@property (nonatomic, strong) NSArray *citys;

/** 所有市下面的区 */
@property (nonatomic, strong) NSArray *areas;

/** 标记正在选择的地区 */
@property (nonatomic, assign) NSInteger index;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftMargin;

/** 当前选择的省对象 */
@property (nonatomic, strong) GYCity *currentProvince;

/** 当前选择的市 */
@property (nonatomic, strong) GYCity *currentCity;

/** 当前选择的区 */
@property (nonatomic, strong) GYCity *currentArea;

/** 当前地区模型 */
@property (nonatomic, strong) GYCity *city;

@end

@implementation GYCitySelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择地区";
    
    //加载省级地区
    [self loadAreaData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.provineceBtu.selected = YES;
    self.index = 1;
}

- (void)loadAreaData {
    //加载省级地区
    [GYCity loadCity:@"0" success:^(NSArray * _Nullable areas, NSError * _Nullable error) {
        if (error) {
            NSLog(@"loadAreasDataError=====%@",error);
        }else {
            self.provinces = areas;
            [self.tableView reloadData];
        }
    }];
}

#pragma mark -- 按钮点击事件

- (IBAction)citySelectBtuClick:(UIButton *)sender {
    switch (sender.tag) {
        case 2:
        {
            if (self.currentProvince == nil) {
                NSLog(@"请您先选择省!");
                return;
            }
        }
            break;
            
        case 3:
        {
            if (self.currentCity == nil) {
                NSLog(@"请您选择市!");
                return;
            }
        }
            
        default:
            break;
    }
    self.index = sender.tag;
    [self reloadLableFrame];
    [self reloadBtuTextColor];
    [self.tableView reloadData];
}

- (IBAction)completeBtuClick:(UIButton *)sender {
    [self returnSelectCity];
}


#pragma mark -- UITableViewDelegate and UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (self.index) {
        case 1:
            return self.provinces.count;
            break;
            
        case 2:
            return self.citys.count;
            break;
            
        case 3:
            return self.areas.count;
            break;
            
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYRegisterCell *cell = [GYRegisterCell cellWithTableView:tableView];
    
    switch (self.index) {
        case 1:
        {
            GYCity *model = self.provinces[indexPath.row];
            cell.cityNameLable.text = model.area_name;
        }
            break;
            
        case 2:
        {
            GYCity *model = self.citys[indexPath.row];
            cell.cityNameLable.text = model.area_name;
        }
            break;
            
        case 3:
        {
            GYCity *model = self.areas[indexPath.row];
            cell.cityNameLable.text = model.area_name;
        }
            break;
            
        default:
            return 0;
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (self.index) {
        case 1:
        {
            self.index = self.index + 1;
            //1.如果选择的是省
            self.currentProvince = self.provinces[indexPath.row];
            
            //2.更换数据源
            [GYCity loadCity:self.currentProvince.area_id success:^(NSArray * _Nullable areas, NSError * _Nullable error) {
                if (error) {
                    NSLog(@"loadAreaDataError===%@",error);
                }else {
                    self.citys = areas;
                    [self.tableView reloadData];
                }
            }];
            
            [self.provineceBtu setTitle:self.currentProvince.area_name forState:UIControlStateNormal];
        }
            break;
            
        case 2:
        {
            //1.如果选择的是市
            self.index = self.index + 1;
            self.currentCity = self.citys[indexPath.row];
            
            //2.更换数据源
            [GYCity loadCity:self.currentCity.area_id success:^(NSArray * _Nullable areas, NSError * _Nullable error) {
                if (error) {
                    NSLog(@"loadAreaDataError===%@",error);
                }else {
                    self.areas = areas;
                    [self.tableView reloadData];
                }
            }];
            
            [self.cityBtu setTitle:self.currentCity.area_name forState:UIControlStateNormal];
        }
            break;
            
        case 3:
        {
            //1.如果选择的是区
            self.currentArea = self.areas[indexPath.row];
            
            [self.areaBtu setTitle:self.currentArea.area_name forState:UIControlStateNormal];
            
            [self returnSelectCity];
        }
            break;
    }
    //3.刷新lable的位置 和按钮字体的颜色
    [self reloadLableFrame];
    [self reloadBtuTextColor];
}

#pragma mark -- 封装的方法

- (void)reloadLableFrame {
    [UIView animateWithDuration:0.5 animations:^{
        self.leftMargin.constant = ScreenWidth/3 * (self.index-1);
    }];
    //刷新界面UI
    [self.view setNeedsLayout];
}

- (void)reloadBtuTextColor {
    switch (self.index) {
        case 1:
        {
            self.provineceBtu.selected = YES;
            self.cityBtu.selected = NO;
            self.areaBtu.selected = NO;
        }
            break;
            
        case 2:
        {
            self.provineceBtu.selected = NO;
            self.cityBtu.selected = YES;
            self.areaBtu.selected = NO;
        }
            break;
            
        case 3:
        {
            self.provineceBtu.selected = NO;
            self.cityBtu.selected = NO;
            self.areaBtu.selected = YES;
        }
            break;
            
        default:
            break;
    }
}

- (void)returnSelectCity {
    NSString *province = self.currentProvince.area_name == nil ? @"" : self.currentProvince.area_name;
    NSString *city = self.currentCity.area_name == nil ? @"" : self.currentCity.area_name;
    NSString *area = self.currentArea.area_name == nil ? @"" : self.currentArea.area_name;
    NSString *address = [NSString stringWithFormat:@"%@ %@ %@",province,city,area];
    self.block(address,self.currentProvince.area_id,self.currentCity.area_id,self.currentArea.area_id);
    //这里要把值传回去
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 懒加载

- (void)dealloc {
    NSLog(@"GYCitySelectViewController====dealloc");
}





@end
